import * as crypto from 'crypto';
// const nodeRSA = require('node-rsa');

// 生成一个1024长度的密钥对
// const key = new nodeRSA({b: 512});
// 导出公钥
// const publicKey = key.exportKey('public');
const publicKey = "-----BEGIN PUBLIC KEY-----\n" +
    "MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBANS/Mu231VLy2QdGm9rznclH5WcbiHno\n" +
    "Lzs2uI+O3w5EE3t90KEmfoUKM8cUCOdImeW0UYHnooIj1oA2n48L9gsCAwEAAQ==\n" +
    "-----END PUBLIC KEY-----\n"
console.log(publicKey);
// 导出私钥
// const privateKey = key.exportKey('private');
const privateKey = "-----BEGIN RSA PRIVATE KEY-----\n" +
    "MIIBOwIBAAJBANS/Mu231VLy2QdGm9rznclH5WcbiHnoLzs2uI+O3w5EE3t90KEm\n" +
    "foUKM8cUCOdImeW0UYHnooIj1oA2n48L9gsCAwEAAQJAf02p+/V1AR2wIFpwCKSe\n" +
    "VkNfUkN3dvsoyBYGemDTuISL/kHgfRIai7DTSFJnp80w1fpBNoicg5UfjhaGnPfk\n" +
    "AQIhAPkxbiv9CailMyBAaqxlH0ZrDvQirf8WPAg1EsMvsbrhAiEA2o7n22DU7RN3\n" +
    "nkbOpi53taOET/9iCh4vpR7QlfPIGmsCICnh8/lyuxfcRD6MdSFqkZodTlMcLRBt\n" +
    "pUNf4DpsHrwhAiEAlY5gnj/+tFQz8Hznf9QgWo0QR2WsgtvxIn6ZoWL2NQ0CIQDr\n" +
    "gZVsFAQbfEHkQxy5Y5XnikA8ZyZerifLRHd+K2PLgg==\n" +
    "-----END RSA PRIVATE KEY-----\n";
console.log(privateKey);
var time = new Date().getTime();
// const secret = '豫EZX123;' + parseInt(String(time / 1000));
const secret = '豫EZX123;1678700439'
// 使用私钥加密，公钥解密
console.log(secret)
// const encrypt = crypto.privateEncrypt(privateKey, Buffer.from(secret));
// const decrypt = crypto.publicDecrypt(publicKey, encrypt);

// 也可反过来
// const encrypt = crypto.publicEncrypt({
//     key: privateKey,
//     padding: crypto.constants.RSA_PKCS1_PADDING // 注意这里的常量值要设置为RSA_PKCS1_PADDING
// }, Buffer.from(secret));

const password = "pi7DLwQjcw2IHulCi/SGcROC2IPcG4ay59Mico4Q5C3whSZq3j0VVWCwm5IeRhwzZMQ/bqYPPXAYKj1iB8BC5w==";
const encrypt = Buffer.from(password, 'base64');
const decrypt = crypto.privateDecrypt({
    key: privateKey,
    padding: crypto.constants.RSA_PKCS1_PADDING // 注意这里的常量值要设置为RSA_PKCS1_PADDING
}, encrypt);

console.log('加密后：', encrypt.toString('base64') + '\n', encrypt.toString('base64').length);
console.log('解密后：', decrypt.toString());
